#ifndef HANDLEREQUEST_H
#define HANDLEREQUEST_H

#include"RquestType.h"
#include"UserManager.h"
#include"ChattingRoom.h"
#include "MySocket.h"
#include"MsgType.h"
#include"NotifyManager.h"
#include"ULMNN_Manager.h"
#include<nlohmann/json.hpp>

using json = nlohmann::json;


auto string_to_json(const std::string& str);
void handle_request(const std::string& request_str,const int& sockfd);

/*处理登录请求,并返回结果
    {
        "request_type":LOGIN,},{"user_id":1},{"pwd":"123456"}
    }

    结果返回
    {
        "response_type":LOGIN,},{"user_id":1},{"result":0},{"msg":"登录成功"}
        {"response_type":LOGIN,},{"user_id":1},{"result":1},{"msg":"用户不存在"}
        {"response_type":LOGIN,},{"user_id":1},{"result":2},{"msg":"密码错误"}
        {"response_type":LOGIN,},{"user_id":1},{"result":3},{"msg":"文件错误"}
    }
*/
void handle_login_request(const json& request_json,const int& sockfd);

/*创建用户请求
    {
        "request_type":CREATE_USER,},{"name":"lin"},{"pwd":"123456"}
    }
        结果返回
    {
        "response_type":CREATE_USER,},{"result":0},{"msg":"创建成功"},{id:1}
    }
*/
void handle_CreateUser_request(const json& request,const int& sockfd);

/*处理消息请求
    {
        {"request_type":SEND_MESSAGE,},{"Msg":Msg_serialize(msg)}
    }
        结果返回
    {
        {"response_type":SEND_MESSAGE,},{"result":0},{"msg":"发送成功"}
    }
*/
void handle_message_request(const json& request_json,const int& sockfd);

/*处理退出请求
    {
        "request_type":LOGOUT,},{"user_id":1}
    }
        结果返回
    {
        "response_type":LOGOUT,},{"user_id":1},{"result":0},{"msg":"退出成功"}
    }
*/
void handle_logout_request(const json& request_json,const int& sockfd);

/*处理加入聊天室请求,向用户聊天列表中加入聊天室
    {
        "request_type":JOIN_CHAT,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        "response_type":JOIN_CHAT,},{"user_id":1},{"chat_room_id":"chat_room_1"},{"result":0},{"msg":"加入成功"}
    }
*/
void handle_joinChat_request(const json& request_json,const int& sockfd);


/*处理创建聊天室请求
    {
        "request_type":CREATE_CHAT,},{"user_id":1},{"chat_room_name":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":CREATE_CHAT,},{"result":0},{"chat_room_id":"chat_room_1"},{"msg":"创建成功"}
    }}
*/
void handle_createChat_request(const int&fd, const json& request_json);

/*处理退出聊天室请求
    {
        "request_type":EXIT_CHAT,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":EXIT_CHAT,},{"result":0},{"chat_room_id":"chat_room_1"},{"msg":"退出成功"}
    }
*/
void handle_exitChat_request(const int&socketfd, const json& request_json);

/*处理删除聊天室请求
    {
        "request_type":DEL_CHAT,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":DEL_CHAT,},{"result":0},{"chat_room_id":"chat_room_1"},{"msg":"删除成功"}
        {"response_type":DEL_CHAT,},{"result":1},{"chat_room_id":"chat_room_1"},{"msg":"删除失败"}
    }
*/
void handle_delChat_request(const int&socketfd, const json& request_json);

/*处理获取聊天室信息请求
    {
        {"request_type":GET_CHAT_INFO,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":GET_CHAT_INFO,},{"result=0"},{chat_room_id:1},{json chat_room_info}}
    }
*/
void handle_getChatInfo_request(const int&socketfd, const json& request_json);


/*处理获取最新信息请求
    {
        {"request_type":GET_LATEST_MSG,},{"user_id":1},{"chat_room_id":"chat_room_1"},{"msg_num":10}
    }
        结果返回
    {
        {"response_type":GET_LATEST_MSG,},{"result=0"},{user_id:1},{list[{msg_type:1},{content:"hello"},{"time":"2021-11-11 11:11:11"}]}
    }
*/
void handle_getLatestMsg_request(const int &sockfd, const json& request_json);

/*处理获取聊天列表请求
    {
        {"request_type":GET_CHAT_LIST,},{"user_id":1}
    }
        结果返回
    {
        {"response_type":GET_CHAT_LIST,},{"result=0"},{user_id:1},{json chat_list}}
    }
*/
void handle_getChatList_request(const json& request_json,const int& sockfd);

/*处理邀请请求
    {
        {"request_type":INVITE_USER,},{"user_id":1},{"chat_room_id":"chat_room_1"},{"invite_user_id":2},{"content":"hello"},{time:"2021-11-11 11:11:11"}
    }
        结果返回
    {
        {"response_type":INVITE_USER,},{"result=0"},{user_id:1},{invite_user_id:2},{"msg":"邀请发送成功"}
}*/
void handle_invite_request(const int&sockfd, const json& request_json);

/*同意邀请请求
    {
        {"request_type":ACCEPT_INVITE,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":ACCEPT_INVITE,},{"result=0"},{user_id:1},{"chat_room_id":"chat_room_1"},{"msg":"加入成功"}
    }
*/
void handle_accept_request(const int &sockfd, const json& request_json);

/*拒绝邀请请求
    {
        {"request_type":REFUSE_INVITE,},{"user_id":1},{"chat_room_id":"chat_room_1"}
    }
        结果返回
    {
        {"response_type":REFUSE_INVITE,},{"result=0"},{user_id:1},{"chat_room_id":"chat_room_1"},{"msg":"拒绝成功"}
}
*/
void handle_refuse_request(const int &sockfd, const json& request_json);



//将客户端的请求结果返回给客户端
void ResponseClient(const int &sockfd, const json &response_json);

void handle_unknown_request(const int& sockfd);



#endif // HANDLEREQUEST_H